import { IFeidaoAiBrowserComponent } from '@feidao/web/interfaces';
import s from './s';
import init from '@feidao/web/framework/browser/dn/component';
import fire_component_msg from '@feidao/web/atom/browser/fsm/fire-component-msg';
import nodejs from '@feidao/web/atom/nodejs/msg-b/nodejs';
import render from '@feidao/web/atom/browser/render/render';
import P01 from './P01';
import P03 from './P03';
import '@feidao/widgets/w-000010';

const feidao = {};
let exam_subjects_id: string = null, class_id: string = null, student_no: string = null, student_ids: string[] = [],
	page_no = 1, page_size = 10, curr_student_no: string = null, curr_upd = {}, curr_row: HTMLDivElement = null;
async function refresh(fd: IFeidaoAiBrowserComponent) {
	const student_name = (fd.data.node.querySelector('[data-feidao-id="student_name"]') as HTMLInputElement).value;
	const res = await nodejs("flippedclass", "distribute_student_find", { flag: 1, student_name, student_ids, class_id, page_size, page_no });
	render(fd, res, P01, 'P01', 'inner');
}
const daoke = {
	async init(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		page_no = 1;
	},
	async ec01(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-distribute-002 init', args);
		curr_student_no = null;
		curr_upd = {};
		curr_row = null;
		(fd.data.node.querySelector('[data-feidao-id="student_name"]') as HTMLInputElement).value = '';
		exam_subjects_id = args[0][0];
		class_id = args[0][1];
		student_no = args[0][2];
		const res = await nodejs("flippedclass", "distribute_student_find", { flag: 0, exam_subjects_id, class_id, page_size, page_no });
		student_ids = res.student_ids;
		render(fd, res, P01, 'P01', 'inner');
		res['page_size'] = page_size;
		render(fd, res, P03, 'P03', 'inner');
		fd.data.node.classList.remove('display-n');
	},
	async eu02(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-distribute-list-001 查询', args);
		page_no = 1;
		refresh(fd);
	},
	async eu03(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-distribute-list-001 分页', args);
		page_no = args[0].page_no;
		refresh(fd);
	},
	async eu04(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-distribute-list-001 选中行', args);
		curr_row && curr_row.classList.remove('bg-ccc');
		curr_row = args[0].currentTarget;
		curr_row.classList.add('bg-ccc');
		curr_student_no = curr_row.dataset.feidaoNo;
		console.log('-------->', curr_student_no);
		curr_upd['student_no'] = curr_student_no;
		curr_upd['sex'] = curr_row.dataset.feidaoSex;
		curr_upd['student_name'] = curr_row.dataset.feidaoName;
		curr_upd['student_type'] = curr_row.dataset.feidaoType;
		curr_upd['student_id'] = curr_row.dataset.feidaoId;
	},
	async eu05(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-distribute-list-001 确定', args);
		if (!curr_student_no) {
			alert('请先选中一条数据！');
			return;
		}
		curr_row.classList.remove('bg-ccc');
		const res = await nodejs('flippedclass', 'distribute_student_upd', curr_upd);
		if (res == 'success') {
			fd.data.node.classList.add('display-n');
			fire_component_msg('zj-distribute-list-001', 'ec02');
		} else {
			alert('操作失败');
		}
	},
	async eu06(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-distribute-list-001 关闭', args);
		fd.data.node.classList.add('display-n');
	}
};

const rules = {};

export default function main(url: string, query: any) {
	return init('zj-distribute-list-002', s, rules, feidao, daoke, url, query, { spaceid: 'flippedclass', systemid: '5991', refsystemid: '@refsystemid' });
}
